import Vue from 'vue'
import Vuex from 'vuex'
//导入module模块中的文件
import feiji from './module/feiji'
import huoche from './module/huoche'
Vue.use(Vuex)

export default new Vuex.Store({
  // strict: true,//严格代码
  state: {
    //定义数据
    count: 10,
    name: '起飞',
    list: [6, 8, 1, 22, 80, 69, 9]
  },
  mutations: {
    //修改数据
    addcount (state, num) {
      state.count *= num
    },

    changeName (state, uname) {
      state.name = uname
    },
  },
  actions: {
    //异步执行代码
    getTime ({ commit }, num1) {
      setTimeout(() => {
        commit('addcount', num1)
      }, 1000)
    }
  },
  getters: {
    //通过state的数据处理一些情况,相对于计算属性
    sum (state) {
      return state.list.reduce((sum, item) => sum + item)
    },
    max (state) {
      return Math.max(...state.list)
    },
    min (state) {
      return Math.min(...state.list)
    },
    zhengxu (state) {
      return state.list.toSorted(function (a, b) {
        return a - b
      })
    },
    daoxu (state) {
      return state.list.toSorted(function (a, b) {
        return b - a
      })
    },
  },
  modules: {
    feiji,
    huoche
  }
})
